import 'package:easy_read_comic/controller/comic/read_controller.dart';
import 'package:easy_read_comic/internal/api/comic_api.dart';
import 'package:easy_read_comic/widgets/auto_image_widget.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' as G;
import 'package:loading_animation_widget/loading_animation_widget.dart';

import '../models/chapter_model.dart';

class ChapterMenuListWidget extends StatefulWidget {
  final ReadController controller;

  ChapterMenuListWidget(this.controller);

  @override
  State<StatefulWidget> createState() => _ChapterMenuListWidget();
}

class _ChapterMenuListWidget extends State<ChapterMenuListWidget> {
  List<ChapterModel> list = [];

  //章节当前的页码
  int currentPage = 0;

  // 章节是否加载完毕
  bool chapterListLoadDone = false;

  @override
  void initState() {
    super.initState();
    getChapterList();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      onRefresh: () async {
        resetGetChapterList();
      },
      onLoad: () async {
        getChapterList();
      },
      child: list.isEmpty
          ? Container(
              child: LoadingAnimationWidget.staggeredDotsWave(
                color: Colors.white,
                size: G.Get.width / 2,
              ),
              alignment: Alignment.center,
              width: G.Get.width,
              height: 200,
            )
          : ListView.builder(
              itemBuilder: (context, index) {
                return GestureDetector(
                  child: Container(
                    width: G.Get.width,
                    margin: EdgeInsets.all(10),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: ClipRRect(
                            child: AutoImage.buildImage(
                              list[index].Thumb,
                            ),
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                          width: G.Get.width / 4 * 1,
                          height: 130,
                        ),
                        Container(
                          padding: EdgeInsets.only(
                            left: 10,
                          ),
                          width: G.Get.width / 4 * 2.7,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                child: Text(
                                  list[index].Title,
                                  maxLines: 1,
                                  style: TextStyle(
                                    fontSize: 20,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                              Container(
                                child: Text(
                                  list[index].Intro,
                                  maxLines: 4,
                                  softWrap: true,
                                  style: TextStyle(
                                    color: Colors.black38,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                  onTap: () {
                    widget.controller
                        .resetLoad(list[index].ComicId, list[index].Id);
                    //关闭打开的弹窗
                    Navigator.pop(context);
                  },
                );
              },
              itemCount: list.length,
            ),
    );
  }

  void getChapterList() {
    if (this.chapterListLoadDone) return;
    ComicApi()
        .chapterList(widget.controller.comicId.toString(),
            currentPage: this.currentPage)
        .then((response) {
      if (response.data["code"] == 1) {
        var data = response.data["data"];
        for (var item in data["List"]) {
          this.list.add(ChapterModel.formJson(item));
        }
        if (data["Page"]["NextPage"] != null) {
          this.currentPage = data["Page"]["NextPage"];
        } else {
          this.chapterListLoadDone = true;
        }
        setState(() {});
      }
    });
  }

  void resetGetChapterList() {
    currentPage = 0;
    chapterListLoadDone = false;
    list = [];
    setState(() {});
    getChapterList();
  }
}
